/*
 * MailAcceptance.java
 *
 * Created on June 2, 2007, 11:08 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package hero.hook;

/**
 *
 * @author kabad
 */
import hero.interfaces.*;
import hero.interfaces.BnNodeLocal;
import hero.util.BonitaServiceException;
import hero.util.HeroHookException;
import hero.util.BonitaServiceLocator;
import hero.util.ServerType;
import javax.mail.MessagingException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.mail.Session;
import javax.mail.Address;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import java.util.*;

public class MailAcceptance implements hero.hook.NodeHookI {
    
    /** Creates a new instance of MailAcceptance */
    public MailAcceptance() {
//         try {
//            initial=new InitialContext();
//            cache = new HashMap();
//        } catch (javax.naming.NamingException ne) {
//            System.out.println("Problemas en la clase: "+ne);
//            //throw new BonitaServiceException(ne.getMessage());
//        }
    }
    
    public String getMetadata() {
        return Constants.Nd.BEFORETERMINATE;
    }
    public void create(Object b,BnNodeLocal n) throws HeroHookException {}
    public void beforeStart(Object b,BnNodeLocal n) throws HeroHookException {}
    public void afterTerminate(Object b,BnNodeLocal n) throws HeroHookException {}
    public void onCancel(Object b,BnNodeLocal n) throws HeroHookException {}
    public void anticipate(Object b,BnNodeLocal n) throws HeroHookException {}
    public void onDeadline(Object b,BnNodeLocal n) throws HeroHookException {}
    public void afterStart(Object b, BnNodeLocal n) throws HeroHookException {}
    public void onReady(Object b,BnNodeLocal n) throws HeroHookException {}
    public void beforeTerminate(Object b,BnNodeLocal n) throws HeroHookException {
        System.out.println("Se ejecuto el hook*******");
//        try {
//            String nodeName = n.getName();
//            BnProjectLocal project = n.getBnProject();
//            String prjName = project.getName();
//            
//            ProjectSessionLocalHome prjhome = (ProjectSessionLocalHome)ProjectSessionUtil.getLocalHome();
//            ProjectSessionLocal prjSession = prjhome.create();
//            prjSession.initProject(prjName);
//            
//            System.out.println("***********************************************************************");
//            System.out.println(nodeName);
////            System.out.println(n.getBnNodePerformerAssign().getName());
////            System.out.println(n.getActivityPerformer());
////
////            BnNodePropertyValue val = prjSession.getNodeProperty(nodeName,"Email");
//            
//            String mailString ="lbeltran@fiec.espol.edu.ec"; //val.getTheValue();
//            BonitaServiceLocator serviceLocator = BonitaServiceLocator.getInstance();
//            Session session;
//            System.out.println("Service locator");
//            session = (Session) serviceLocator.getMailSession(BonitaServiceLocator.Services.MAIL_SERVICE);
//            System.out.println("Ya obtubo la session");
//            MimeMessage m = new MimeMessage(session);
//            m.setFrom();
//            Address[] to = new InternetAddress[] {new InternetAddress(mailString)};
//            System.out.println("Direccion: "+to[0]);
//            m.setRecipients(javax.mail.Message.RecipientType.TO, to);
//            m.setSubject("Guess what???");
//            m.setSentDate(new java.util.Date());
//            String content = "Ya nos salio el envio de mail utilizando el engine de bonita\nAtentamente Larry Beltran";
//            m.setContent(content,"text/plain");
//// Sending email
//            Transport.send(m);
//            System.out.println("Email was successfully sent");
//        } catch (BonitaServiceException e) {
//            System.out.print(e.getMessage());
//            System.out.println("mail service error: "+e);e.printStackTrace();
//        }catch(MessagingException m)
//        {
//            System.out.println("Mensaje exception: "+ m.getMessage());
//        }
//        catch (Exception e) {
//            System.out.print(e.getMessage());
//            System.out.println("*****mail service error: "+e);e.printStackTrace();
//        }
    }
    
    
//    private static BonitaServiceLocator msl;
//    private Map cache;
//    private static Context initial;
//    
//    
//
//
//    // Services constant inner class - service objects
//    
//    public class Services {
//        final public static int TOPIC_CONNECTION_FACTORY=0;
//        final public static int TOPIC=1;
//        final public static int MAIL_SERVICE=2;
//    }
//    
//    // JMS related constants
//    // NB: It was decided not to change the name of the factory in
//    // JBoss configuration to match JOnAS name, hence the test.
//    final static String TOPIC_CONNECTION_FACTORY_NAME = ServerType.isJonas() ? "TCF" : "java:/XAConnectionFactory";
//    final static String TOPIC_NAME = "testTopic";
//    
//    // Mail configuration
//    final static String MAIL_SERVICE_NAME = "Mail";
//    
//    // Returns the JNDI name for the required service
//    
//    
//    static private  String getServiceName(int service) {
//        switch (service) {
//            case Services.TOPIC_CONNECTION_FACTORY: return TOPIC_CONNECTION_FACTORY_NAME;
//            case Services.TOPIC: return TOPIC_NAME;
//            case Services.MAIL_SERVICE: return MAIL_SERVICE_NAME;
//        }
//        return null;
//    }
//    
//    // returns a EJBHome for the given service, using the JNDI name
//    // and the Class for the EJBHome
//    
//    public Object getResource(int s) throws BonitaServiceException {
//        Object resource=null;
//        try {
//            String serv = getServiceName(s);
//            if (cache.containsKey(serv)) {
//                resource = (Object) cache.get(serv);
//            } else {
//                Object objref = initial.lookup(serv);
//                resource=objref;
//                cache.put(serv, resource);
//            }
//        } catch (NamingException ex) {
//            throw new BonitaServiceException("naming: "+ex.getExplanation()+"ServiceException\n");
//        } catch (Exception ex) {
//            System.out.println("ServiceLocator Exception");
//            ex.printStackTrace();
//        }
//        
//        return resource;
//    }
//    
//    public Object getMailSession(int s) throws BonitaServiceException {
//        Enumeration<?> claves;
//        Hashtable<?, ?> medioAmbiente;
//        Object mail=null;
//        try {
//            String serv = getServiceName(s);
//            System.out.println("Nombre del Servicio: "+serv);
//            if (cache.containsKey(serv)) {
//                System.out.println("Lo tenia en cache");
//                mail = (Object) cache.get(serv);
//            } else {
//                System.out.println(" NO lo  tenia en cache");
//                
//                medioAmbiente=initial.getEnvironment();
//                claves=medioAmbiente.keys();
//                while (claves.hasMoreElements()){
//                    Object o=claves.nextElement();
//                    if (o instanceof String){
//                    System.out.println("NameInNamespace:  "+o);
//                    }
//                }
//                
//                
//                Object objref = initial.lookup(serv);
//                System.out.println("objref:  "+objref);
//                mail= PortableRemoteObject.narrow(objref,Session.class);
//                cache.put(serv, mail);
//            }
//        } catch (NamingException ex) {
//            throw new BonitaServiceException("\nNaming "+ex.getExplanation() + "  \nMensaje :  "+ex.getMessage()+"ServiceException\n");
//        } catch (Exception ex) {
//            System.out.println("ServiceLocator Exception");
//            ex.printStackTrace();
//        }
//        
//        return mail;
//    }
    
}
